package uk.co.cocking.getinline2.builder;

import java.util.List;

import uk.co.cocking.getinline2.pipeline.io.CouldNotProcessRecordException;
import uk.co.cocking.getinline2.pipeline.io.UrlRetriever;
import uk.co.cocking.getinline2.pipeline.transformers.AbstractTransformer;
import uk.co.cocking.getinline2.record.StringField;
import uk.co.cocking.getinline2.record.MapRecord;
import uk.co.cocking.getinline2.record.Record;

public class UrlMerger extends AbstractTransformer<Record, Record> {

	private final String newContentFieldName;
	private final String urlFieldName;
	private final UrlRetriever retriever = new UrlRetriever();

	public UrlMerger(String newContentFieldName, String urlFieldName) {
		this.newContentFieldName = newContentFieldName;
		this.urlFieldName = urlFieldName;
	}

	@Override
	public List<Record> transform(Record incomingRecord)
			throws CouldNotProcessRecordException {
		String url = incomingRecord.getString(urlFieldName);
		String content = retriever.transform(url).get(0);
		return enlist(new MapRecord(incomingRecord, new StringField(newContentFieldName,content)));
	}

}
